{% extends "developpements/base.html" %}

{% load sexyfication %}

{% block extrajs %}
    {{ block.super }}
    <script type="text/javascript">
        function onClosePopup(win) {
            win.close();
            window.location.reload();
        }
        function popupModifyOpenTd(triggeringLink) {
            href = triggeringLink
            if (href.indexOf('?') == -1) {
                href += '?_popup=1';
            } else {
                href  += '&_popup=1';
            }
            var win = window.open(href, 'modifypopup', 'height=500,width=1000,resizable=yes,scrollbars=yes');
            win.focus();
            return false;
        }
        function popupModifyOpen(triggeringLink) {
            href = triggeringLink.href
            if (href.indexOf('?') == -1) {
                href += '?_popup=1';
            } else {
                href  += '&_popup=1';
            }
            var win = window.open(href, 'modifypopup', 'height=500,width=1000,resizable=yes,scrollbars=yes');
            win.focus();
            return false;
        }
        dojo.addOnLoad(function() {
            dojo.query("td.modifiable").forEach(function(ligne)
                {
                var itempk = dojo.attr(ligne, "itempk");
                var contextMenu = new dijit.Menu({
                    targetNodeIds: [ligne]
                    });
                contextMenu.addChild(new dijit.MenuItem({
                    label: "Modifier",
                    onClick: function(event) { 
                        popupModifyOpenTd(dojo.attr(ligne, 'url'));
                    },
                    }));
                if (dojo.attr(ligne, 'itempk'))
                    {
                    // C'est un dev, pas un groupedev
                    contextMenu.addChild(new dijit.MenuItem({
                        label: "Noter comme étant terminé",
                        onClick: function(event)
                            {
                            var defer = dojo.xhrGet({
                                url : "{% url developpements.views.done %}",
                                handleAs : "json",
                                content : {
                                    dev_pk : itempk,
                                    },
                                load: function (response)
                                    {
                                    if (response.error)
                                        {
                                        alert (response.error);
                                        }
                                    else
                                        {
                                        var limage = dojo.create('img');
                                        dojo.attr(limage, 'src', '{{ MEDIA_URL }}icones/bool-True.png');
                                        dojo.place(limage, "nom_" + response.dev_pk, "first");
                                        }
                                    }
                                });
                            }
                        }));
                    contextMenu.addChild(new dijit.MenuItem({
                        label: "Noter comme étant un bug",
                        onClick: function(event)
                            {
                            var defer = dojo.xhrGet({
                                url : "{% url developpements.views.bug %}",
                                handleAs : "json",
                                content : {
                                    dev_pk : itempk,
                                    },
                                load: function (response)
                                    {
                                    if (response.error)
                                        {
                                        alert (response.error);
                                        }
                                    else
                                        {
                                        var limage = dojo.create('img');
                                        dojo.attr(limage, 'src', '{{ MEDIA_URL }}icones/bug.png');
                                        dojo.place(limage, "nom_" + response.dev_pk, "first");
                                        }
                                    }
                                });
                            }
                        }));
                    contextMenu.addChild(new dijit.MenuItem({
                        label: "Pas de couleur",
                        onClick: function(event)
                            {
                            var defer = dojo.xhrGet({
                                url : "{% url developpements.views.change_color %}",
                                handleAs : "json",
                                content : {
                                    dev_pk : itempk,
                                    },
                                load: function (response)
                                    {
                                    if (response.error)
                                        {
                                        alert (response.error);
                                        }
                                    else if (response.couleur)
                                        {
                                        dojo.style("nom_" + response.dev_pk, "background-color", '#' + response.couleur);
                                        }
                                    else
                                        {
                                        dojo.style("nom_" + response.dev_pk, "background-color", 'inherit');
                                        }
                                    }
                                });
                            }
                        }));
                    {% for couleur in couleurs %}
                        contextMenu.addChild(new dijit.MenuItem({
                            label: "Couleur {{ couleur }}",
                            style: "background-color: #{{ couleur }};",
                            onClick: function(event)
                                {
                                var defer = dojo.xhrGet({
                                    url : "{% url developpements.views.change_color %}",
                                    handleAs : "json",
                                    content : {
                                        dev_pk : itempk,
                                        couleur : "{{ couleur }}"
                                        },
                                    load: function (response)
                                        {
                                        if (response.error)
                                            {
                                            alert (response.error);
                                            }
                                        else if (response.couleur)
                                            {
                                            dojo.style("nom_" + response.dev_pk, "background-color", '#' + response.couleur);
                                            }
                                        else
                                            {
                                            dojo.style("nom_" + response.dev_pk, "background-color", 'inherit');
                                            }
                                        }
                                    });
                                }
                            }));
                    {% endfor %}
                    }
                contextMenu.startup();
                });
            });
        function save_item_field(itempk, type, value_type, newvalue)
            {
            dojo.style('change_' + type + '_' + itempk, "background-color", "red");
            var defer = dojo.xhrGet({
                url : "{% url developpements.views.save_item_field %}",
                handleAs : "json",
                content : {
                    dev_pk : itempk,
                    field_type : type,
                    value_type : value_type,
                    newvalue : newvalue,
                    },
                load: function (response)
                    {
                    if (response.error)
                        {
                        alert (response.error);
                        }
                    else
                        {
                        dojo.style("change_" + type + "_" + itempk, "backgroundColor", "green");
                        if (response.new_poids_total)
                            {
                            dojo.attr(dojo.byId("poids_total_" + itempk), "value", response.new_poids_total);
                            dojo.attr(dojo.byId("poids_groupe_" + itempk), "title", response.new_poids_total);
                            dojo.attr(dojo.byId("poids_manuel_" + itempk), "title", response.new_poids_total);
                            move_developpement(itempk, response.new_poids_total);
                            }
                        }
                    },
                error: function(error)
                    {
                    alert (error);
                    }
                });
            }
        function move_developpement(itempk, poids_total)
            {
            var this_ligne = dojo.byId('developpement_ligne_' + itempk);
            var this_poids_total = poids_total;
            var table_dev = dojo.byId('table_dev');
            this_ligne.parentNode.removeChild(this_ligne);
            var poids_precedent;
            var found_place = false;
            dojo.query("tbody tr", table_dev).forEach(function(tr, index)
                {
                var other_poids_total = dojo.attr(dojo.query("input.poids_total", tr)[0], "value");
                if ((poids_precedent && other_poids_total < this_poids_total && poids_precedent > this_poids_total) || (!poids_precedent && other_poids_total < this_poids_total))
                    {
                    found_place = true;
                    dojo.place(this_ligne, tr, "before");
                    }
                poids_precedent = other_poids_total;
                });
            if (!found_place)
                {
                dojo.place(this_ligne, table_dev);
                }
            }
        function change_version_requise(itempk)
            {
            var dialog = new dijit.Dialog({
                title : 'Choix de la version',
                style: 'width: 350px;',
                });
            dialog.attr('content', '<ul><li><a href="#" onClick="choose_new_version(' + itempk + ', 0, \'' + dialog.id + '\');">Aucune</a></li>{% for vexist in versions_existantes %}<li><a href="#" onClick="choose_new_version(' + itempk + ', {{ vexist.id }}, \'' + dialog.id + '\');">{{ vexist.majeur }}.{{ vexist.mineur }}.{{ vexist.revision }}</a></li>{% endfor %}</ul>');
            dialog.show();
            }
        function change_version_present(itempk)
            {
            var dialog = new dijit.Dialog({
                title : 'Choix de la version',
                style: 'width: 350px;',
                });
            dialog.attr('content', '<ul><li><a href="#" onClick="choose_present_version(' + itempk + ', 0, \'' + dialog.id + '\');">Aucune</a></li>{% for vexist in versions_existantes %}<li><a href="#" onClick="choose_present_version(' + itempk + ', {{ vexist.id }}, \'' + dialog.id + '\');">{{ vexist.majeur }}.{{ vexist.mineur }}.{{ vexist.revision }}</a></li>{% endfor %}</ul>');
            dialog.show();
            }
        function choose_new_version(itempk, versionpk, dialog_id)
            {
            dojo.style('change_version_requise_' + itempk, "background-color", "red");
            var defer = dojo.xhrGet({
                url : "{% url developpements.views.save_item_field %}",
                handleAs : "json",
                content : {
                    dev_pk : itempk,
                    field_type : 'version_requise',
                    value_type : 'integer',
                    newvalue : versionpk,
                    },
                load: function (response)
                    {
                    if (response.error)
                        {
                        alert (response.error);
                        }
                    else
                        {
                        dojo.style("change_version_requise_" + itempk, "backgroundColor", "green");
                        dojo.attr('change_version_requise_' + itempk, 'innerHTML', response.innerHTML);
                        dijit.byId(dialog_id).hide();
                        }
                    },
                error: function(error)
                    {
                    alert (error);
                    }
                });
            }
        function choose_present_version(itempk, versionpk, dialog_id)
            {
            dojo.style('change_version_present_' + itempk, "background-color", "red");
            var defer = dojo.xhrGet({
                url : "{% url developpements.views.save_item_field %}",
                handleAs : "json",
                content : {
                    dev_pk : itempk,
                    field_type : 'version_present',
                    value_type : 'integer',
                    newvalue : versionpk,
                    },
                load: function (response)
                    {
                    if (response.error)
                        {
                        alert (response.error);
                        }
                    else
                        {
                        dojo.style("change_version_present_" + itempk, "backgroundColor", "green");
                        dojo.attr('change_version_present_' + itempk, 'innerHTML', response.innerHTML);
                        dijit.byId(dialog_id).hide();
                        }
                    },
                error: function(error)
                    {
                    alert (error);
                    }
                });
            }
        function only_show_this_groupe(group_pk)
            {
            var table = dojo.byId('table_dev');
            dojo.query("tbody tr", table).forEach(function(tr, index)
                {
                if (dojo.query("td.groupe_dev_" + group_pk, tr).length == 0)
                    {
                    var display = dojo.style(tr, "display");
                    dojo.style(tr, "display", (display == "table-row" ? "none" : "table-row"));
                    }
                });
            }
        function show_dev_dialog(itempk)
            {
            var dialog;
            var dialog_id = 'dev_dialog_for_item_' + itempk;
            dialog = dijit.byId(dialog_id);
            if (!dialog)
                {
                dialog = new dijit.Dialog({
                    'id' : dialog_id,
                    'title' : 'Details',
                    'href' : '/developpements/dev_dialog/' + itempk + '/',
                    style: 'width: 600px;',
                    });
                }
            dialog.refresh();
            dialog.show();
            }
        function un_accent(str)
            {
            var new_string = String(str);
            new_string = new_string.replace(/[èéêë]/g,"e");
            new_string = new_string.replace(/[àäâ]/g,"a");
            new_string = new_string.replace(/[ùüû]/g,"u");
            new_string = new_string.replace(/[öô]/g,"o");
            new_string = new_string.replace(/[ïî]/g,"i");
            return new_string;
            }
        function filter_dev(value)
            {
            /* filtre la liste des dev pour n'afficher que ceux qui ont ce mot cle */
            var table = dojo.byId('table_dev');
            dojo.query("tbody tr", table).forEach(function(tr, index)
                {
                dojo.style(tr, "display", ((un_accent(tr.innerHTML.toLowerCase()).indexOf(un_accent(value.toLowerCase())) != -1) ? "table-row" : "none"));
                });
            }
    </script>
{% endblock %}

{% block content %}
    <ul class="object-tools"><li><a href="{% url admin:developpements_developpement_add %}?project={{ project.id }}" onclick="return popupModifyOpen(this);">Ajouter</a></li>
    </ul>
    {{ block.super }}
    <input type="text" onChange="filter_dev(this.value);"/>
    <table id="table_dev">
        <thead>
            <tr style="background-color: #C0C0C0;">
                <th>&nbsp;</th>
                {# poids total <th>&nbsp;</th> #}
                <th title="Cliens demandeurs">Clients</th>
                <th title="Poids de l'item">P</th>
                <th>Groupe</th>
                <th title="Poids du groupe">PG</th>
                <th title="Poids manuel">PM</th>
                <th>Libellé</th>
                <th title="Temps prévu (heures)">Temps</th>
                <th title="Version requise">VR</th>
                <th title="Date engagement">Eng</th>
                <th title="Date de sortie possible">Sortie</th>
                <th>&nbsp;</th>
            </tr>
        </thead>
        <tbody>
            {% for item in developpements %}
                <tr id="developpement_ligne_{{ item.pk }}">
                    <td class="align-center" style="background-color: #C0C0C0;"><input type="hidden" class="poids_total" id="poids_total_{{ item.pk }}" value="{{ item.poids_total }}"/><a href="{% url admin:developpements_developpement_change item.pk %}?project={{ project.id }}" onclick="return popupModifyOpen(this);">{{ forloop.counter }}</a></td>
                    <td class="align-right" title="{{ item.clients|join:" " }}">{{ item.clients|length }}</td>
                    <td class="align-right">
                        <span id="change_poids_{{ item.pk }}" dojoType="dijit.InlineEditBox" onChange="save_item_field({{ item.pk }}, 'poids', 'integer', this.value);" autoSave="true">{{ item.poids }}</span>
                    </td>
                    <td class="groupe_dev_{{ item.groupe.pk }} {% if perms.developpements.change_groupedev %}modifiable{% endif %}" url="{% url admin:developpements_groupedev_change item.groupe.pk %}" title="{{ item.groupe.description }}">
                        <a href="javascript:only_show_this_groupe({{ item.groupe.pk }});">{{ item.groupe }}</a>
                    </td>
                    <td id="poids_groupe_{{ item.pk }}" class="align-right" title="{{ item.poids_total }}">
                        <span id="change_poids_groupe_{{ item.pk }}" dojoType="dijit.InlineEditBox" onChange="save_item_field({{ item.pk }}, 'poids_groupe', 'integer', this.value);" autoSave="true">{{ item.groupe.poids }}</span>
                    </td>
                    <td id="poids_manuel_{{ item.pk }}" class="align-right" title="{{ item.poids_total }}">
                        <span id="change_poids_manuel_{{ item.pk }}" dojoType="dijit.InlineEditBox" onChange="save_item_field({{ item.pk }}, 'poids_manuel', 'float', this.value);" autoSave="true"> {{ item.poids_manuel }}</span>
                    </td>
                    <td class="{% if perms.developpements.change_developpement %}modifiable{% endif %}{% if item.done %} done{% endif %}" url="{% url admin:developpements_developpement_change item.pk %}?project={{ project.id }}"
                        title="{{ item.description }}"
                        itempk="{{ item.pk }}"
                        id="nom_{{ item.pk }}"
                        {% if item.couleur %}style="background-color: #{{ item.couleur }};"{% endif %}>
                        <a href="javascript:show_dev_dialog({{ item.pk }});"><img src="{{ MEDIA_URL }}icones/xeyes.png" height="16"/></a>
                        {% if item.done %}{{ item.done|boolicon }}{% endif %}
                        {% if item.bug %}<img src="{{ MEDIA_URL }}icones/bug.png"/>&nbsp;{% endif %}
                        <span id="change_nom_{{ item.pk }}" dojoType="dijit.InlineEditBox" onChange="save_item_field({{item.pk}}, 'nom', 'string', this.value);" autoSave="true">{{ item.nom }}</span>
                        {# <div dojoType="dijit.Dialog" title="{{ item.nom }}" id="dialog_{{ item.pk }}">{{ item.description|urlize }}</div> #}
                    </td>
                    <td class="align-right">
                        <span id="change_temps_prevu_{{ item.pk }}" dojoType="dijit.InlineEditBox" onChange="save_item_field({{ item.pk }}, 'temps_prevu', 'integer', this.value);" autoSave="true">{{ item.temps_prevu|default_if_none:"&empty;" }}</span>
                    </td>
                    <td>
                        <a id="change_version_requise_{{ item.pk }}" href="javascript:void();" onClick="change_version_requise({{ item.pk }}, this);">{{ item.version_requise|default_if_none:"&nbsp;" }}</a>
                    </td>
                    <td class="align-center">{{ item.engagement|date:"d/m/Y" }}</td>
                    <td class="align-center" title="{{ item.date_sortie|date:"d/m/Y" }}">
                        <a id="change_version_present_{{ item.pk }}" href="javascript:void();" onClick="change_version_present({{ item.pk }}, this);">
                        {% if item.deja_dispo %}
                            {{ item.deja_dispo }}
                        {% else %}{% if item.done %}
                            Proch. version
                        {% else %}
                            Sem. {{ item.date_sortie|date:'W' }}
                        {% endif %}{% endif %}</a>
                    </td>
                    <td class="align-center">
                        {% if item.alerte %}
                            <img src="{{ MEDIA_URL }}icones/incendie.png" title="{{ item.alerte }}" alt="alerte"/>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}
